package com.example.administrator.daydayappmarket.game.fragment.fragments.choiceness.adapter;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.administrator.daydayappmarket.Contacts;
import com.example.administrator.daydayappmarket.R;
import com.example.administrator.daydayappmarket.game.fragment.fragments.choiceness.activity.GameChoiceListViewSecondActivity;
import com.example.administrator.daydayappmarket.game.fragment.fragments.choiceness.bean.GameChoiceMainInfo;
import com.example.administrator.daydayappmarket.game.fragment.fragments.choiceness.bean.GameChoiceMainInfoParent;
import com.example.administrator.daydayappmarket.game.fragment.fragments.choiceness.bean.GameChoiceRefreshHeader;
import com.example.administrator.daydayappmarket.recommend.activity.RecommendTopImageSpecialThirdActivity;
import com.squareup.picasso.Picasso;

import java.util.List;

import butterknife.ButterKnife;
import butterknife.InjectView;

/**
 * Created by 郎志奇 on 2016/12/26.
 */

public class GameChoiceListViewAdapter extends BaseAdapter {



    private List<GameChoiceMainInfoParent> data;

    private Context context;

    public GameChoiceListViewAdapter(List<GameChoiceMainInfoParent> data, Context context) {
        this.data = data;
        this.context = context;
    }

    @Override
    public int getCount() {
        return data != null ? data.size() : 0;
    }

    @Override
    public GameChoiceMainInfoParent getItem(int position) {
        return data.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public int getViewTypeCount() {
        return 3;
    }

    @Override
    public int getItemViewType(int position) {

        int type = 0;

        GameChoiceMainInfoParent item = getItem(position);
        String theme_type = item.getTheme_type();

        if (theme_type.equals("apps")) {
            type = 0;
        }
        if (theme_type.equals("pic_topic")) {
            type = 1;
        }

        return type;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        int itemViewType = getItemViewType(position);


        ViewHolder holder = null;
        ViewHolderHeader holderHeader = null;

        if (convertView == null) {

            switch (itemViewType) {

                case 0:
                    convertView = LayoutInflater.from(context).inflate(R.layout.item_fragment_game_choice_list, parent, false);

                    holder = new ViewHolder(convertView);

                    convertView.setTag(holder);
                    break;
                case 1:

                    convertView = LayoutInflater.from(context).inflate(R.layout.item_game_choice_refresh_header, parent, false);

                    holderHeader = new ViewHolderHeader(convertView);

                    convertView.setTag(holderHeader);
                    break;
            }


        } else {

            switch (itemViewType) {

                case 0:
                    holder = (ViewHolder) convertView.getTag();
                    break;
                case 1:
                    holderHeader = (ViewHolderHeader) convertView.getTag();
                    break;
            }

        }

        GameChoiceMainInfoParent item = getItem(position);

        switch (itemViewType) {

            case 0:
                final GameChoiceMainInfo gameChoiceMainInfo = (GameChoiceMainInfo) item;
                holder.mItemFragmentGameChoiceListTextName
                        .setText(gameChoiceMainInfo.getName());

                holder.mItemFragmentGameChoiceListTextReviews
                        .setText(gameChoiceMainInfo.getReviews());

                holder.mItemFragmentGameChoiceListDownloadsCountsTextSize
                        .setText(Integer.parseInt(gameChoiceMainInfo.getDownload_count()) / 10000 + "万人下载   "
                                + Integer.parseInt(gameChoiceMainInfo.getPackage_size()) / (1024 * 1024) + "MB");


                if(gameChoiceMainInfo.getIcon_url().startsWith("http")){

                    Picasso.with(context).load(gameChoiceMainInfo.getIcon_url())
                            .into(holder.mItemFragmentGameChoiceListImage);

                }else {

                    Picasso.with(context).load(Contacts.IMAGE_PREFIX + gameChoiceMainInfo.getIcon_url())
                            .into(holder.mItemFragmentGameChoiceListImage);
                }

                holder.linearLayout.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {

                        Intent intent=new Intent(context, GameChoiceListViewSecondActivity.class);

                        intent.putExtra("id",gameChoiceMainInfo.getId());

                        context.startActivity(intent);
                    }
                });


                break;
            case 1:

                final GameChoiceRefreshHeader gameChoiceRefreshHeader = (GameChoiceRefreshHeader) item;

                holderHeader.mItemGameChoiceRefreshHeaderTextView1.setText(
                        gameChoiceRefreshHeader.getTopic_title()
                );

                holderHeader.mItemGameChoiceRefreshHeaderTextView2.setText(
                        gameChoiceRefreshHeader.getTopic_name()
                );

                holderHeader.mItemGameChoiceRefreshHeaderTextView1
                        .setBackgroundColor(Color.parseColor(gameChoiceRefreshHeader.getTitle_color()));


                if(gameChoiceRefreshHeader.getImage().startsWith("http")){

                    Picasso.with(context).load(gameChoiceRefreshHeader.getImage())
                            .into(holderHeader.mItemGameChoiceRefreshHeaderImageView);

                }else {

                    Picasso.with(context).load(Contacts.IMAGE_PREFIX + gameChoiceRefreshHeader.getImage())
                            .into(holderHeader.mItemGameChoiceRefreshHeaderImageView);
                }


                holderHeader.mItemGameChoiceRefreshHeaderImageView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {

                        Intent intent=new Intent(context, RecommendTopImageSpecialThirdActivity.class);

                        intent.putExtra("id",gameChoiceRefreshHeader.getId());

                        context.startActivity(intent);
                    }
                });

                break;

        }

        //设置数据

//        holder.mItemFragmentGameChoiceListTextName
//                .setText(data.get(position).getName());
//
//        holder.mItemFragmentGameChoiceListTextReviews
//                .setText(data.get(position).getReviews());
//
//        holder.mItemFragmentGameChoiceListDownloadsCountsTextSize
//                .setText(Integer.parseInt(data.get(position).getDownload_count())/10000+"万人下载   "
//                        +Integer.parseInt(data.get(position).getPackage_size())/(1024*1024)+"MB");

//        Picasso.with(context).load(Contacts.IMAGE_PREFIX+data.get(position).getIcon_url())
//                .into(holder.mItemFragmentGameChoiceListImage);

        return convertView;
    }




    static class ViewHolder {
        @InjectView(R.id.item_fragment_game_choice_list_image)
        ImageView mItemFragmentGameChoiceListImage;

        @InjectView(R.id.item_fragment_game_choice_list_text_name)
        TextView mItemFragmentGameChoiceListTextName;


        @InjectView(R.id.item_fragment_game_choice_list_download_button)
        Button mItemFragmentGameChoiceListDownloadButton;

        @InjectView(R.id.item_fragment_game_choice_list_downloads_counts_text_size)
        TextView mItemFragmentGameChoiceListDownloadsCountsTextSize;

        @InjectView(R.id.item_fragment_game_choice_list_text_reviews)
        TextView mItemFragmentGameChoiceListTextReviews;

        @InjectView(R.id.item_game_choice_listView_linearLayout)
        LinearLayout linearLayout;

        public ViewHolder(View convertView) {
            ButterKnife.inject(this, convertView);
        }
    }

    static class ViewHolderHeader {
        @InjectView(R.id.item_game_choice_refresh_header_textView1)
        TextView mItemGameChoiceRefreshHeaderTextView1;
        @InjectView(R.id.item_game_choice_refresh_header_textView2)
        TextView mItemGameChoiceRefreshHeaderTextView2;
        @InjectView(R.id.item_game_choice_refresh_header_imageView)
        ImageView mItemGameChoiceRefreshHeaderImageView;

        ViewHolderHeader(View view) {
            ButterKnife.inject(this, view);
        }
    }
}
